package org.pps.gl.client.vue;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.LoginInfo;
import org.pps.gl.client.Pps_gl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Button;

/**
 * Composite pour l'envoi d'un message à un utilisateur
 *
 */
public class PFL_V_NMessages extends Composite {

	public PFL_V_NMessages(final String emailVisite) {

		FlexTable flexTable = new FlexTable();
		initWidget(flexTable);
		flexTable.setSize("230px", "126px");

		Label lblNewLabel = new Label("Objet");
		flexTable.setWidget(0, 0, lblNewLabel);

		final TextBox textBox = new TextBox();
		flexTable.setWidget(0, 1, textBox);
		textBox.setWidth("210px");

		Label lblNewLabel_1 = new Label("Message");
		flexTable.setWidget(1, 0, lblNewLabel_1);

		final TextArea textArea = new TextArea();
		flexTable.setWidget(1, 1, textArea);
		textArea.setSize("210px", "72px");

		Button btnNewButton = new Button("New button");
		btnNewButton.setText("Envoyer");
		flexTable.setWidget(2, 1, btnNewButton);
		
		btnNewButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GetInfoServiceAsync gi = GWT
						.create(GetInfoService.class);
				gi.addMessage(emailVisite, LoginInfo.getCurrentUser().getEmailAddress(), textArea.getText(), textBox.getText(),
						new AsyncCallback<Boolean>() {
						@Override
						public void onSuccess(Boolean result) {
							// On rafraîchie les données sur la page
							Pps_gl.getCm().refresh();
							textArea.setText("");
							textBox.setText("");
							GWT.log("Message envoyé.");
						}
						@Override
						public void onFailure(Throwable caught) {
							GWT.log("Erreur lors de l'envoi du message.");
							Window.alert("Message non envoyé");
						}
					});
			}
		});
	}

}
